package com.zhnl.zhnl_web.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.zhnl.zhnl_web.bean.BstLeaveWordPojo;
import com.zhnl.zhnl_web.mapper.BstMessageMapper;
import com.zhnl.zhnl_web.service.BstMessageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author 罗兴发
 * @data 2025/9/17 15:39
 * @description：客户留言-业务实现类
 */
@Service
@Slf4j
public class BstMessageServiceImpl implements BstMessageService {

    @Resource
    BstMessageMapper bstMessageMapper;

    /**
     * 添加留言
     * @param json
     * @return
     */
    @Override
    public String addMessage(JSONObject json) {
        JSONObject js = new JSONObject();

        // 获取参数
        String name = json.getString("name");
        String phone = json.getString("phone");
        String content = json.getString("content");

        // 时间规格化
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String addTime = sf.format(new Date());

        // 对象实例化
        BstLeaveWordPojo w = new BstLeaveWordPojo();
        w.setCustomerName(name);
        w.setCustomerPhone(phone);
        w.setCustomerNeed(content);
        w.setAddTime(addTime);

        // 添加数据
        int flag = bstMessageMapper.addMessage(w);
        if (flag<0){
            js.put("code",4017);
            js.put("msg","fails");
        }else {
            js.put("code",200);
            js.put("msg","success");
        }

        return JSONObject.toJSONString(js, SerializerFeature.WriteMapNullValue);
    }
}
